package com.betterjr.modules.sys.security;

import org.apache.shiro.authc.AuthenticationToken;

/**
 * 用于C2(现金管理平台)授权登录到本系统
 * create by zhoul 2018-07-05
 */
public class BetterSsoToken implements AuthenticationToken {

    /**
     * 默认2分钟过期时间
     */
    private Long expireTime = 2 * 60 * 1000L;

    /**
     * 用户代码
     */
    private String userCode;
    /**
     * 机构号
     */
    private String corpCode;
    /**
     * 标识该token的有效时间戳
     */
    private Long timestamp;

    public BetterSsoToken(String userCode, String corpCode, Long timestamp) {
        this.userCode = userCode;
        this.corpCode = corpCode;
        this.timestamp = timestamp;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getCorpCode() {
        return corpCode;
    }

    public void setCorpCode(String corpCode) {
        this.corpCode = corpCode;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public Object getPrincipal() {
        return this.getUserCode();
    }

    @Override
    public Object getCredentials() {
        return this.getCorpCode();
    }

    /**
     * 判断是否过期
     * @return
     */
    public boolean isExpire() {
        return ((System.currentTimeMillis() - getTimestamp()) > expireTime);
    }
}
